#include "toolbardeclaration.h"
#include <algorithm>

namespace yc {

ToolBarDeclaration::ToolBarDeclaration(const QString& title, Qt::ToolBarArea area,
                                       const QSize& icon_size)
    : title_(title), area_(area), icon_size_(icon_size)
{
}

void ToolBarDeclaration::addItem(const QString& name, const QString& path)
{
    if (path.isEmpty()) {
        items_.push_back(Item { name });
    } else {
        QStringList sl = path.split('/', Qt::SkipEmptyParts, Qt::CaseSensitive);
        QList<Item>* p = &items_;
        for (const auto& s : sl) {
            auto it =
                    std::find_if(p->begin(), p->end(), [s](const Item& i) { return i.name == s; });
            if (it != p->end()) {
                p = &(*it).children;
            } else {
                p = nullptr;
                break;
            }
        }
        if (p) {
            p->push_back(Item { name });
        }
    }
}

} // namespace yc
